package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.logi.sys.bo.LogiCustomerQueryBo;
import com.piggy.logi.sys.bo.LogiProjectShipEditBo;
import com.piggy.logi.sys.bo.LogiProjectShipQueryBo;
import com.piggy.logi.sys.domain.LogiProjectShip;
import com.piggy.logi.sys.mapper.LogiProjectShipMapper;
import com.piggy.logi.sys.service.ILogiCustomerService;
import com.piggy.logi.sys.service.ILogiProjectShipService;
import com.piggy.logi.sys.vo.LogiCustomerExtVo;
import com.piggy.logi.sys.vo.LogiProjectShipExtVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目收发货管理Service业务层处理
 *
 * @author piggy
 * @date 2023-08-18
 */
@Slf4j
@Service
public class LogiProjectShipServiceImpl extends ServiceImpl<LogiProjectShipMapper, LogiProjectShip> implements ILogiProjectShipService {

    @Resource
    private ILogiCustomerService customerService;

    @Override
    public LogiProjectShipExtVo queryById(Long id){
        LogiProjectShipExtVo vo = getVoById(id, LogiProjectShipExtVo.class);
        if (ObjectUtil.isEmpty(vo)) {
            return vo;
        }
        return fillData(Lists.newArrayList(vo)).get(0);
    }

    @Override
    public TableDataInfo<LogiProjectShipExtVo> queryPageList(LogiProjectShipQueryBo bo) {
        PagePlus<LogiProjectShip, LogiProjectShipExtVo> result = pageVo(PageUtils.buildPagePlus(bo, true), buildQueryWrapper(bo), LogiProjectShipExtVo.class);
        result.setRecordsVo(fillData(result.getRecordsVo()));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiProjectShipExtVo> queryList(LogiProjectShipQueryBo bo) {
        return fillData(listVo(buildQueryWrapper(bo), LogiProjectShipExtVo.class));
    }

    private LambdaQueryWrapper<LogiProjectShip> buildQueryWrapper(LogiProjectShipQueryBo bo) {
        LambdaQueryWrapper<LogiProjectShip> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, LogiProjectShip::getId, bo.getId());
        lqw.eq(bo.getProjectId() != null, LogiProjectShip::getProjectId, bo.getProjectId());
        lqw.eq(StrUtil.isNotBlank(bo.getShipFlag()), LogiProjectShip::getShipFlag, bo.getShipFlag());
        lqw.eq(bo.getCustomerId() != null, LogiProjectShip::getCustomerId, bo.getCustomerId());
        lqw.in(CollUtil.isNotEmpty(bo.getProjectIdList()), LogiProjectShip::getProjectId, bo.getProjectIdList());
        lqw.in(CollUtil.isNotEmpty(bo.getIdList()), LogiProjectShip::getId, bo.getIdList());
        return lqw;
    }

    public List<LogiProjectShipExtVo> fillData(List<LogiProjectShipExtVo> extVos) {
        LinkedHashMap<Long,LogiProjectShipExtVo> shipMap = extVos.stream().collect(LinkedHashMap::new, (m, v) -> m.put(v.getId(), v), LinkedHashMap::putAll);

        List<Long> customerIds = extVos.stream().map(LogiProjectShipExtVo::getCustomerId).distinct().collect(Collectors.toList());
        customerIds.add(-1L);

        List<LogiCustomerExtVo> customerExtVoList = customerService.queryList(new LogiCustomerQueryBo().setCustomerIdList(customerIds));

        LinkedHashMap<Long,LogiCustomerExtVo> cusMap = customerExtVoList.stream().collect(LinkedHashMap::new, (m, v) -> m.put(v.getCustomerId(), v), LinkedHashMap::putAll);

        extVos.stream().peek(Any-> {
            shipMap.get(Any.getId()).setCustomer(cusMap.get(Any.getCustomerId()));
        }).collect(Collectors.toList());

        return Lists.newArrayList(shipMap.values());

    }

    @Override
    public Boolean insertByAddBo(LogiProjectShipEditBo bo) {
        LogiProjectShip add = BeanUtil.toBean(bo, LogiProjectShip.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(LogiProjectShipEditBo bo) {
        LogiProjectShip update = BeanUtil.toBean(bo, LogiProjectShip.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchByAddBo(List<LogiProjectShipEditBo> boList) {
        if (CollUtil.isEmpty(boList)) {
            return false;
        }

        List<LogiProjectShip> bookList = BeanUtil.copyToList(boList, LogiProjectShip.class);
        return this.saveBatch(bookList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchByUpdateBo(List<LogiProjectShipEditBo> boList) {
        if (CollUtil.isEmpty(boList)) {
            return false;
        }

        this.remove(Wrappers.<LogiProjectShip>lambdaQuery().eq(LogiProjectShip::getProjectId, boList.get(0).getProjectId()));
        return this.saveOrUpdateBatch(BeanUtil.copyToList(boList, LogiProjectShip.class));
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(LogiProjectShip entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
